package org.dromara.answer.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.answer.domain.vo.AiUserExamsVo;
import org.dromara.answer.domain.bo.AiUserExamsBo;
import org.dromara.answer.service.IAiUserExamsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户考试记录
 *
 * @author Lion Li
 * @date 2025-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/answer/userExams")
public class AiUserExamsController extends BaseController {

    private final IAiUserExamsService aiUserExamsService;

    /**
     * 查询用户考试记录列表
     */
    @SaCheckPermission("answer:userExams:list")
    @GetMapping("/list")
    public TableDataInfo<AiUserExamsVo> list(AiUserExamsBo bo, PageQuery pageQuery) {
        return aiUserExamsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户考试记录列表
     */
    @SaCheckPermission("answer:userExams:export")
    @Log(title = "用户考试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiUserExamsBo bo, HttpServletResponse response) {
        List<AiUserExamsVo> list = aiUserExamsService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户考试记录", AiUserExamsVo.class, response);
    }

    /**
     * 获取用户考试记录详细信息
     *
     * @param userExamId 主键
     */
    @SaCheckPermission("answer:userExams:query")
    @GetMapping("/{userExamId}")
    public R<AiUserExamsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userExamId) {
        return R.ok(aiUserExamsService.queryById(userExamId));
    }

    /**
     * 新增用户考试记录
     */
    @SaCheckPermission("answer:userExams:add")
    @Log(title = "用户考试记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiUserExamsBo bo) {
        return toAjax(aiUserExamsService.insertByBo(bo));
    }

    /**
     * 修改用户考试记录
     */
    @SaCheckPermission("answer:userExams:edit")
    @Log(title = "用户考试记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiUserExamsBo bo) {
        return toAjax(aiUserExamsService.updateByBo(bo));
    }

    /**
     * 删除用户考试记录
     *
     * @param userExamIds 主键串
     */
    @SaCheckPermission("answer:userExams:remove")
    @Log(title = "用户考试记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userExamIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userExamIds) {
        return toAjax(aiUserExamsService.deleteWithValidByIds(List.of(userExamIds), true));
    }
}
